package com.abel.bigwater.dataserver.model.ewide

import com.abel.bigwater.data.BcdString
import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.serializer.SerializerFeature
import io.netty.buffer.Unpooled
import org.junit.Assert
import org.junit.Test
import org.slf4j.LoggerFactory

class TermUploadReqDataTest {

    @Test
    fun parseBuf() {
        val buf = Unpooled.buffer().apply { writeBytes(BcdString.toByteArray("70FFFF898A")) }
        val tud = TermUploadReqData(5)

        assert(tud.parseBuf(buf))
        Assert.assertEquals(0xFFFF, tud.frameCount)
        Assert.assertArrayEquals(arrayOf(KmingChannelType.Flow4851, KmingChannelType.Flow4852),
                tud.channelList.toArray())
    }

    @Test
    fun pack() {
        val tud = TermUploadReqData().apply {
            frameCount = 0xFFFF
            channelList = arrayListOf(KmingChannelType.Flow4851, KmingChannelType.Flow4852)
        }

        val buf = Unpooled.buffer()
        tud.pack(buf)

        Assert.assertEquals(5, buf.readableBytes())
        Assert.assertArrayEquals(byteArrayOf(0x70, 0xFF.toByte(), 0xFF.toByte(), 0x89.toByte(), 0x8A.toByte()),
                ByteArray(5).also { buf.readBytes(it) })
    }

    @Test
    fun packWhole() {
        val p = KmingPacket().apply {
            termCode = TermCodeType().also {
                it.manuId = BcdString.fromBcdString("03")
                it.codeBcd = BcdString.fromBcdString("01000000")
            }

            deviceStatus = DeviceStatus().also {
                it.lowBattery10 = true
                it.magnet21 = true
                it.breakLine30 = true
            }

            frameNo = 1
            data = TermUploadReqData().also {
                it.frameCount = 10
                it.channelList = arrayListOf(KmingChannelType.Total4851,
                        KmingChannelType.Flow4851, KmingChannelType.PulseRevert1,
                        KmingChannelType.Pressure1)
            }
        }

        val buf = Unpooled.buffer().apply {
            p.pack(this)

            Assert.assertEquals("5A55010000000301020101000700700A0085898D872EEC6A69", BcdString.hexString(this).toUpperCase())
        }
    }

    @Test
    fun parseWhole() {
        val buf = Unpooled.buffer().apply { writeBytes(BcdString.toByteArray("5A55010000000301020101000700700A0085898D872EEC6A69")) }
        val p = KmingPacket()
        Assert.assertTrue(p.parseBuf(buf))

        lgr.info(JSON.toJSONStringWithDateFormat(p, JSON.DEFFAULT_DATE_FORMAT, SerializerFeature.PrettyFormat))

        Assert.assertEquals(TermCodeType().apply {
            manuId = BcdString.fromBcdString("03")
            codeBcd = BcdString.fromBcdString("01000000")
        }, p.termCode)

        val data = p.data as TermUploadReqData
        Assert.assertEquals(10, data.frameCount)
        Assert.assertArrayEquals(arrayOf(KmingChannelType.Total4851,
                KmingChannelType.Flow4851, KmingChannelType.PulseRevert1,
                KmingChannelType.Pressure1),
                data.channelList.toArray())
    }

    companion object {
        private val lgr = LoggerFactory.getLogger(TermUploadReqDataTest::class.java)
    }
}